// Keefer 2007 IO

//REPLICATION 
clear
use "K2007 IO Rep Data.dta"
summarize
display c(k)
mdesc
tabmiss
codebook ifs
codebook year

sort ifs year
by ifs: gen gdppp_1=gdppp[_n-1]
by ifs: gen gdppp_2=gdppp[_n-2]
by ifs: gen gdppp_3=gdppp[_n-3]
by ifs: gen gdppp_4=gdppp[_n-4]
egen gdpav_2=rmean(gdppp_1 gdppp_2)
egen gdpav_3=rmean(gdppp_1 gdppp_2 gdppp_3)
egen gdpav_4=rmean(gdppp_1 gdppp_2 gdppp_3 gdppp_4)

by ifs: gen gdppp1=gdppp[_n+1]
by ifs: gen gdppp2=gdppp[_n+2]
by ifs: gen gdppp3=gdppp[_n+3]
by ifs: gen gdppp4=gdppp[_n+4]

egen gdpav2=rmean(gdppp1 gdppp2)
egen gdpav3=rmean(gdppp1 gdppp2 gdppp3)
egen gdpav4=rmean(gdppp1 gdppp2 gdppp3 gdppp4)

egen gdpav11=rmean(gdppp1 gdppp_1)
egen gdpav22=rmean(gdpav2 gdpav_2)
egen gdpav33=rmean(gdpav3 gdpav_3)

sort ifs year
by ifs: gen gdpg_1=gdppc_gr[_n-1]
by ifs: gen gdpg_2=gdppc_gr[_n-2]
by ifs: gen gdpg_3=gdppc_gr[_n-3]
by ifs: gen gdpg_4=gdppc_gr[_n-4]

by ifs: gen gdpg1=gdppc_gr[_n+1]
by ifs: gen gdpg2=gdppc_gr[_n+2]
by ifs: gen gdpg3=gdppc_gr[_n+3]
by ifs: gen gdpg4=gdppc_gr[_n+4]

egen gdpgav_2=rmean(gdpg_1 gdpg_2)
egen gdpgav_3=rmean(gdpg_1 gdpg_2 gdpg_3)
egen gdpgav_4=rmean(gdpg_1 gdpg_2 gdpg_3 gdpg_4)

egen gdpgav2=rmean(gdpg1 gdpg2)
egen gdpgav3=rmean(gdpg1 gdpg2 gdpg3)
egen gdpgav4=rmean(gdpg1 gdpg2 gdpg3 gdpg4)

sort ifs year

by ifs:  gen curr_acct_1=curr_acct[_n-1]
by ifs:  gen curr_acct_2=curr_acct[_n-2]
by ifs:  gen curr_acct_3=curr_acct[_n-3]
by ifs:  gen curr_acct_4=curr_acct[_n-4]

egen curr_acctav_2=rmean(curr_acct_1 curr_acct_2)
egen curr_acctav_3=rmean(curr_acct_1 curr_acct_2 curr_acct_3)
egen curr_acctav_4=rmean(curr_acct_1 curr_acct_2 curr_acct_3 curr_acct_4)

gen curr_acctdel_1 = (curr_acct - curr_acct_1)	
*change in curr account as percent of GDP
gen curr_acctdel_2 = (curr_acct - curr_acct_2)

sort ifs year

by ifs:  gen curr_acct1=curr_acct[_n+1]
by ifs:  gen curr_acct2=curr_acct[_n+2]
by ifs:  gen curr_acct3=curr_acct[_n+3]
by ifs:  gen curr_acct4=curr_acct[_n+4]

egen curr_acctav2=rmean(curr_acct1 curr_acct2)
egen curr_acctav3=rmean(curr_acct1 curr_acct2 curr_acct3)
egen curr_acctav4=rmean(curr_acct1 curr_acct2 curr_acct3 curr_acct4)

sort ifs year

by ifs:  gen external_balance_1=external_balance[_n-1]
by ifs:  gen external_balance_2=external_balance[_n-2]
by ifs:  gen external_balance_3=external_balance[_n-3]
by ifs:  gen external_balance_4=external_balance[_n-4]

egen external_balanceav_2=rmean(external_balance_1 external_balance_2)
egen external_balanceav_3=rmean(external_balance_1 external_balance_2 external_balance_3)
egen external_balanceav_4=rmean(external_balance_1 external_balance_2 external_balance_3 external_balance_4)

gen external_balancedel_1 = (external_balance - external_balance_1)	
*change in external bal as percent of GDP
gen external_balancedel_2 = (external_balance - external_balance_2) 
sort ifs year

by ifs:  gen external_balance1=external_balance[_n+1]
by ifs:  gen external_balance2=external_balance[_n+2]
by ifs:  gen external_balance3=external_balance[_n+3]
by ifs:  gen external_balance4=external_balance[_n+4]

egen external_balanceav2=rmean(external_balance1 external_balance2)
egen external_balanceav3=rmean(external_balance1 external_balance2 external_balance3)
egen external_balanceav4=rmean(external_balance1 external_balance2 external_balance3 external_balance4)

sort ifs year

by ifs:  gen  deficit_gdp_gr_miss_1 =  deficit_gdp_gr_miss[_n-1] 
by ifs: gen deficit_gdp_gr_miss_2 = deficit_gdp_gr_miss[_n-2] 
by ifs: gen deficit_gdp_gr_miss_3 = deficit_gdp_gr_miss[_n-3] 

egen def_gdp_gr_missav_3 = rmean(deficit_gdp_gr_miss_1 deficit_gdp_gr_miss_2 deficit_gdp_gr_miss_3)  

by ifs:  gen  deficit_gdp_gr_miss1 =  deficit_gdp_gr_miss[_n+1] 
by ifs: gen deficit_gdp_gr_miss2 = deficit_gdp_gr_miss[_n+2] 
by ifs: gen deficit_gdp_gr_miss3 = deficit_gdp_gr_miss[_n+3] 

egen def_gdp_gr_missav3 = rmean(deficit_gdp_gr_miss1 deficit_gdp_gr_miss2 deficit_gdp_gr_miss3)  



sort ifs year

by ifs:  gen debt_export_1=debt_export[_n-1]
by ifs:  gen debt_export_2=debt_export[_n-2]
by ifs:  gen debt_export_3=debt_export[_n-3]
by ifs:  gen debt_export_4=debt_export[_n-4]

egen debt_exportav_2=rmean(debt_export_1 debt_export_2)
egen debt_exportav_3=rmean(debt_export_1 debt_export_2 debt_export_3)
egen debt_exportav_4=rmean(debt_export_1 debt_export_2 debt_export_3 debt_export_4)

sort ifs year

by ifs:  gen debt_export1=debt_export[_n+1]
by ifs:  gen debt_export2=debt_export[_n+2]
by ifs:  gen debt_export3=debt_export[_n+3]
by ifs:  gen debt_export4=debt_export[_n+4]

egen debt_exportav2=rmean(debt_export1 debt_export2)
egen debt_exportav3=rmean(debt_export1 debt_export2 debt_export3)
egen debt_exportav4=rmean(debt_export1 debt_export2 debt_export3 debt_export4)

sort ifs year

by ifs:  gen real_interest_1=real_interest[_n-1]
by ifs:  gen real_interest_2=real_interest[_n-2]
by ifs:  gen real_interest_3=real_interest[_n-3]
by ifs:  gen real_interest_4=real_interest[_n-4]

egen real_interestav_2=rmean(real_interest_1 real_interest_2)
egen real_interestav_3=rmean(real_interest_1 real_interest_2 real_interest_3)
egen real_interestav_4=rmean(real_interest_1 real_interest_2 real_interest_3 real_interest_4)

sort ifs year

by ifs:  gen real_interest1=real_interest[_n+1]
by ifs:  gen real_interest2=real_interest[_n+2]
by ifs:  gen real_interest3=real_interest[_n+3]
by ifs:  gen real_interest4=real_interest[_n+4]

egen real_interestav2=rmean(real_interest1 real_interest2)
egen real_interestav3=rmean(real_interest1 real_interest2 real_interest3)
egen real_interestav4=rmean(real_interest1 real_interest2 real_interest3 real_interest4)

sort ifs year

by ifs:  gen reser_debt_1=reser_debt[_n-1]
by ifs:  gen reser_debt_2=reser_debt[_n-2]
by ifs:  gen reser_debt_3=reser_debt[_n-3]
by ifs:  gen reser_debt_4=reser_debt[_n-4]

egen reser_debtav_2=rmean(reser_debt_1 reser_debt_2)
egen reser_debtav_3=rmean(reser_debt_1 reser_debt_2 reser_debt_3)
egen reser_debtav_4=rmean(reser_debt_1 reser_debt_2 reser_debt_3 reser_debt_4)

sort ifs year

by ifs:  gen reser_debt1=reser_debt[_n+1]
by ifs:  gen reser_debt2=reser_debt[_n+2]
by ifs:  gen reser_debt3=reser_debt[_n+3]
by ifs:  gen reser_debt4=reser_debt[_n+4]

egen reser_debtav2=rmean(reser_debt1 reser_debt2)
egen reser_debtav3=rmean(reser_debt1 reser_debt2 reser_debt3)
egen reser_debtav4=rmean(reser_debt1 reser_debt2 reser_debt3 reser_debt4)
sort ifs year

by ifs:  gen terms_trade_1=terms_trade[_n-1]
by ifs:  gen terms_trade_2=terms_trade[_n-2]
by ifs:  gen terms_trade_3=terms_trade[_n-3]

gen terms_tradedel_1=(terms_trade - terms_trade_1)/terms_trade_1
gen terms_tradedel_2=(terms_trade - terms_trade_2)/terms_trade_2
gen terms_tradedel1_2=(terms_trade_1 - terms_trade_2)/terms_trade_2

sort ifs year

by ifs:  gen terms_trade1=terms_trade[_n+1]
by ifs:  gen terms_trade2=terms_trade[_n+2]
by ifs:  gen terms_trade3=terms_trade[_n+3]

gen terms_tradedel1=(terms_trade1 - terms_trade)/terms_trade
gen terms_tradedel2=(terms_trade2 - terms_trade)/terms_trade
gen terms_tradedel2_1=(terms_trade2 - terms_trade1)/terms_trade1

sort ifs year

by ifs:  gen service_export_1=service_export[_n-1]
by ifs:  gen service_export_2=service_export[_n-2]
by ifs:  gen service_export_3=service_export[_n-3]
by ifs:  gen service_export_4=service_export[_n-4]

egen service_exportav_2=rmean(service_export_1 service_export_2)
egen service_exportav_3=rmean(service_export_1 service_export_2 service_export_3)
egen service_exportav_4=rmean(service_export_1 service_export_2 service_export_3 service_export_4)

gen service_exportdel_1=(service_export - service_export_1)/service_export_1

sort ifs year

by ifs:  gen service_export1=service_export[_n+1]
by ifs:  gen service_export2=service_export[_n+2]
by ifs:  gen service_export3=service_export[_n+3]
by ifs:  gen service_export4=service_export[_n+4]

egen service_exportav2=rmean(service_export1 service_export2)
egen service_exportav3=rmean(service_export1 service_export2 service_export3)
egen service_exportav4=rmean(service_export1 service_export2 service_export3 service_export4)

sort ifs year

by ifs:  gen expt_gdp_1=expt_gdp[_n-1]
by ifs:  gen expt_gdp_2=expt_gdp[_n-2]
by ifs:  gen expt_gdp_3=expt_gdp[_n-3]
by ifs:  gen expt_gdp_4=expt_gdp[_n-4]

egen expt_gdpav_2=rmean(expt_gdp_1 expt_gdp_2)
egen expt_gdpav_3=rmean(expt_gdp_1 expt_gdp_2 expt_gdp_3)
egen expt_gdpav_4=rmean(expt_gdp_1 expt_gdp_2 expt_gdp_3 expt_gdp_4)

sort ifs year

by ifs:  gen expt_gdp1=expt_gdp[_n+1]
by ifs:  gen expt_gdp2=expt_gdp[_n+2]
by ifs:  gen expt_gdp3=expt_gdp[_n+3]
by ifs:  gen expt_gdp4=expt_gdp[_n+4]

egen expt_gdpav2=rmean(expt_gdp1 expt_gdp2)
egen expt_gdpav3=rmean(expt_gdp1 expt_gdp2 expt_gdp3)
egen expt_gdpav4=rmean(expt_gdp1 expt_gdp2 expt_gdp3 expt_gdp4)

egen expt_gdpav33 = rmean(expt_gdpav_3 expt_gdpav3)

* BANKING VARS
sort ifs year

by ifs:  gen priv_1=prvt_dsit_bank[_n-1]
by ifs:  gen priv_2=prvt_dsit_bank[_n-2]
by ifs:  gen priv_3=prvt_dsit_bank[_n-3]
egen privav_3=rmean(priv_1 priv_2 priv_3)
sort ifs year
by ifs:  gen privgr_13=(1/2)*ln(priv_1/priv_3)
by ifs:  gen privgr_03=(1/3)*ln(prvt_dsit_bank/priv_3)

*POLITICAL VARS
sort ifs year 

by ifs: gen stabs_1=stabns[_n-1]
by ifs: gen stabs_2=stabns[_n-2]
by ifs: gen stabs_3=stabns[_n-3]
by ifs: gen stabs_4=stabns[_n-4]

egen stabsav_4=rmean(stabs_1 stabs_2 stabs_3 stabs_4)
egen stabsav_3=rmean(stabs_1 stabs_2 stabs_3)
egen stabsav_2=rmean(stabs_1 stabs_2)
egen stabsav_23=rmean(stabs_2 stabs_3)

by ifs: gen stabs1=stabns[_n+1]
by ifs: gen stabs2=stabns[_n+2]
by ifs: gen stabs3=stabns[_n+3]
by ifs: gen stabs4=stabns[_n+4]

egen stabsav4=rmean(stabs1 stabs2 stabs3 stabs4)
egen stabsav3=rmean(stabs1 stabs2 stabs3)
egen stabsav2=rmean(stabs1 stabs2)

sort ifs year
by ifs: gen check_1=checks[_n-1]
by ifs: gen check_2=checks[_n-2]
by ifs: gen check_3=checks[_n-3]
by ifs: gen check_4=checks[_n-4]

egen checkav_4=rmean(check_1 check_2 check_3 check_4)
egen checkav_3=rmean(check_1 check_2 check_3)
egen checkav_2=rmean(check_1 check_2)

sort ifs year

by ifs: gen check1=checks[_n+1]
by ifs: gen check2=checks[_n+2]
by ifs: gen check3=checks[_n+3]
by ifs: gen check4=checks[_n+4]

egen checkav4= rmean(check1 check2 check3 check4)
egen checkav3=rmean(check1 check2 check3)
egen checkav2=rmean(check1 check2)

egen checkav11=rmean(check1 check_1)
egen checkav22=rmean(checkav2 checkav_2)
egen checkav33=rmean(checkav3 checkav_3)

sort ifs year
gen eiec_dum=eiec
replace eiec_dum=1 if eiec>6 & eiec~=.
replace eiec_dum=0 if eiec<=6 & eiec~=.
by ifs: gen eiec_1=eiec_dum[_n-1]
by ifs: gen eiec_2=eiec_dum[_n-2]
by ifs: gen eiec_3=eiec_dum[_n-3]
by ifs: gen eiec_4=eiec_dum[_n-4]

egen eiecav_4=rmean(eiec_1 eiec_2 eiec_3 eiec_4)
egen eiecav_3=rmean(eiec_1 eiec_2 eiec_3)
egen eiecav_2=rmean(eiec_1 eiec_2)

by ifs: gen eiec1=eiec_dum[_n+1]
by ifs: gen eiec2=eiec_dum[_n+2]
by ifs: gen eiec3=eiec_dum[_n+3]
by ifs: gen eiec4=eiec_dum[_n+4]

egen eiecav4=rmean(eiec1 eiec2 eiec3 eiec4)
egen eiecav3=rmean(eiec1 eiec2 eiec3)
egen eiecav2=rmean(eiec1 eiec2)

egen eiecav11=rmean(eiec1 eiec_1)
egen eiecav22=rmean(eiecav2 eiecav_2)
egen eiecav33=rmean(eiecav3 eiecav_3)

reg checkav33 eiecav33 if fcostgdp~=.
predict checkav33_res, resid

gen gdpg_del33=gdpgav3-gdpgav_3

// Summary statistics for effective dataset
drop if missing(fcostgdp)
summarize
display c(k)
mdesc
codebook ifs
codebook year

*TABLE 4:  core fcost results
reg fcostgdp checkav33_res , cluster(ifs)
codebook year if e(sample)
codebook ifs if e(sample)
mdesc fcostgdp checkav33_res

reg fcostgdp checkav33_res eiecav33 stabsav_3, cluster(ifs)
codebook year if e(sample)
codebook ifs if e(sample)
mdesc fcostgdp checkav33_res eiecav33 stabsav_3

reg fcostgdp checkav33_res eiecav33 stabsav_3 curr_acct_1 curr_acctdel_1 , cluster(ifs)
codebook year if e(sample)
codebook ifs if e(sample)
mdesc fcostgdp checkav33_res eiecav33 stabsav_3 curr_acct_1 curr_acctdel_1

reg fcostgdp checkav33_res eiecav33 stabsav_3 reser_debt_1 curr_acct_1 curr_acctdel_1 , cluster(ifs)
codebook year if e(sample)
codebook ifs if e(sample)
mdesc fcostgdp checkav33_res eiecav33 stabsav_3 reser_debt_1 curr_acct_1 curr_acctdel_1

reg fcostgdp checkav33_res eiecav33 stabsav_3 terms_tradedel_1 gdpav33 gdpgav_3 , cluster(ifs)
codebook year if e(sample)
codebook ifs if e(sample)
mdesc fcostgdp checkav33_res eiecav33 stabsav_3 terms_tradedel_1 gdpav33 gdpgav_3

reg fcostgdp checkav33_res eiecav33 stabsav_3 reser_debt_1 curr_acct_1 curr_acctdel_1 gdpgav_3 gdpav33 terms_tradedel_1, cluster(ifs)
codebook year if e(sample)
codebook ifs if e(sample)
mdesc fcostgdp checkav33_res eiecav33 stabsav_3 reser_debt_1 curr_acct_1 curr_acctdel_1 gdpgav_3 gdpav33 terms_tradedel_1

// IMPUTATION
clear
use "K2007 IO Imp Data.dta"
summarize

mi import flong, m(imp) id(ifs case) imp(fcostgdp-terms_tradedel_1)

*TABLE 4:  core fcost results
mi estimate, post: reg fcostgdp checkav33_res , cluster(ifs)

mi estimate, esampvaryok post: reg fcostgdp checkav33_res eiecav33 stabsav_3, cluster(ifs)

mi estimate, esampvaryok post: reg fcostgdp checkav33_res eiecav33 stabsav_3 curr_acct_1 curr_acctdel_1 , cluster(ifs)

mi estimate, esampvaryok post: reg fcostgdp checkav33_res eiecav33 stabsav_3 reser_debt_1 curr_acct_1 curr_acctdel_1 , cluster(ifs)

mi estimate, esampvaryok post: reg fcostgdp checkav33_res eiecav33 stabsav_3 terms_tradedel_1 gdpav33 gdpgav_3 , cluster(ifs)

mi estimate, esampvaryok post: reg fcostgdp checkav33_res eiecav33 stabsav_3 reser_debt_1 curr_acct_1 curr_acctdel_1 gdpgav_3 gdpav33 terms_tradedel_1, cluster(ifs)

